<?php
class News_model extends CI_Model {
	private $table = 'news';

	function getManagement($limit,$page){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by("created","DESC");
		$query = $this->db->get(PREFIX.$this->table);

		foreach ($query->result() as $row){
			$result[] = $row;
		}
		
		if(!empty($result)){
			return $result;
		}else{
			return false;
		}
	}
	
	function getTotalManagement(){
		$this->db->select('id');
		$this->db->order_by("created","DESC");
		$query = $this->db->get(PREFIX.$this->table);

		foreach ($query->result() as $row){
			$result[] = $row;
		}
		
		if(!empty($result)){
			return count($result);
		}else{
			return false;
		}
	}
	
	function getsearchContent($limit,$page){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by("created","DESC");
		if($this->input->post('content')!=''){
			$this->db->like('title', $this->input->post('content')); 
			$this->db->or_like('type', $this->input->post('content')); 
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);

		foreach ($query->result() as $row){
			$result[] = $row;
		}
		
		if(!empty($result)){
			return $result;
		}else{
			return false;
		}
	}
	
	function getTotalsearchContent(){
		$this->db->select('*');
		$this->db->order_by("created","DESC");
		if($this->input->post('content')!=''){
			$this->db->like('title', $this->input->post('content')); 
			$this->db->or_like('type', $this->input->post('content')); 
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);

		foreach ($query->result() as $row){
			$result[] = $row;
		}
		
		if(!empty($result)){
			return count($result);
		}else{
			return false;
		}
	}
	
	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		foreach ($query->result() as $row){
			$result[] = $row;
		}
		
		if(!empty($result)){
			return $result;
		}else{
			return false;
		}
	}
	
	function saveManagement($image=''){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}
		if($this->input->post('hiddenIDManagement')==0){
			$data = array(
				'image'=> $image,
				'title'=> $this->input->post('titleAdmincp'),
				'description'=> $this->input->post('descAdmincp'),
				'content'=> $this->input->post('contentAdmincp'),
				'type'=> $this->input->post('typeAdmincp'),
				'status'=> $status,
				'created'=> date('Y-m-d H:i:s',time()),
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				return true;
			}
		}else{
			$data = array(
				'title'=> $this->input->post('titleAdmincp'),
				'description'=> $this->input->post('descAdmincp'),
				'content'=> $this->input->post('contentAdmincp'),
				'type'=> $this->input->post('typeAdmincp'),
				'status'=> $status,
			);
			$this->db->where('id',$this->input->post('hiddenIDManagement'));
			if($this->db->update(PREFIX.$this->table,$data)){
				return true;
			}
		}
		return false;
	}
	
	function delManagement($id){
		if($id!=0){
			$this->db->where('id',$id);
			if($this->db->delete(PREFIX.$this->table)){
				return true;
			}
			return false;
		}
	}
}